﻿using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

namespace Core.Res
{
    public partial class EditorLoader
    {
        public class Asset : LoadAwaiter, IAsset
        {
            private readonly EditorLoader loader;

            public UnityEngine.Object Obj { get; private set; }

            public RefCnt Ref { get; }
            public Stack<GameObject> Pool { get; set; }

            public Asset(EditorLoader loader) : base(loader)
            {
                this.loader = loader;

                Pool = new Stack<GameObject>();

                Ref = new RefCnt();
            }

            public void SetInfo(string asset_name)
            {
                Name = asset_name;
            }

            public override void Start()
            {
                if (loader.assets.ContainsKey(Name))
                {
                    State = ELoadState.Complete;
                    return;
                }

                Obj = LoadAssetAtPath(Name);
                if (Obj == null)
                {
                    State = ELoadState.Complete;
                    Debug.LogError($"AssetVO.Error Path={Name}");
                    loader.missings.Add(Name);
                    return;
                }

                loader.assets[Name] = this;
                loader.missings.Remove(Name);

                State = ELoadState.Complete;
                Finish();
            }

            public override void Update()
            {
            }


            public override void Finish()
            {
                //Debug.LogError($"AssetVO.Finish Path={Name}");
                Awaiter?.SetResult();
                Awaiter = null;
            }


            public void Dispose()
            {
                State = ELoadState.Invalid;
                Obj = null;
            }

            public void LoadSync()
            {
                Obj = LoadAssetAtPath(Name);
                if (Obj == null)
                {
                    Debug.LogError($"ERROR 资源不存在 path={Name}");
                    loader.missings.Add(Name);
                    return;
                }

                loader.assets[Name] = this;
                loader.missings.Remove(Name);
            }

            public UnityEngine.Object LoadAssetAtPath(string name)
            {
#if UNITY_EDITOR
                if (loader.APIMode == ApiMode.AssetDatabase)
                {
                    return AssetDatabase.LoadAssetAtPath(name, typeof(UnityEngine.Object));
                }
                else if (loader.APIMode == ApiMode.Resource)
                {
                    return Resources.Load(name);
                }
#else
                if (loader.APIMode == ApiMode.Resource)
                {
                    return Resources.Load(name);
                }
                else
                {
                    log.err($"当前平台不支持AssetDatabase模式");
                    return null;
                }
#endif
                return null;
            }
        }
    }
}
